public class MyList implements IList{
    class ListNode {
        public int data;//数据域
        public ListNode next;

        public ListNode(int data) {
            this.data = data;
        }
    }
    public ListNode head;//定位头的位置

    public void createNode() {
        ListNode node1 = new ListNode(10);
        ListNode node2 = new ListNode(20);
        ListNode node3 = new ListNode(30);

        node1.next = node2;
        node2.next = node3;

        this.head = node1;
    }

    @Override
    public void addFirst(int data) {
        //头插
        ListNode node = new ListNode(data);

        node.next = head;//
        head = node;
    }

    @Override
    public void addLast(int data) {
        //尾插
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while(cur.next!=null) {
            cur = cur.next;
        }
        cur.next = node;

    }

    @Override
    public void add(int index, int data) {
        //指定位置插入
        ListNode node = new ListNode(data);
        //1.检查位置是否合法
        if(index < 0 || index > size()) {
            System.out.println("插入位置不合法");
            return;
        }
        //2.空链表情况
        if(head == null) {
            head = node;
            return;
        }
        //3.头插法情况（index = 0）
        if(index == 0) {
            addFirst(data);
            return;
        }
        //4.找前位置
        int count = index-1;
        ListNode cur = head;
        while(count > 0) {
            cur = cur.next;
            count--;
        }
        //5.插入数据
        node.next = cur.next;
        cur.next = node;
    }

    @Override
    public boolean contains(int key) {
        //查找key元素
        ListNode cur = head;
        while(cur!=null) {
            if(cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        //删除第一个key
        //1.空表
        if(head == null) {
            System.out.println("链表为空，无法删除");
            return;
        }
        //2.头结点就是目标结点
        if(head.data == key) {
            head = head.next;
            return;
        }
        //3.正常删除
        ListNode cur = head;
        while(cur.next!=null) {
            if(cur.next.data==key) {
                cur.next = cur.next.next;//删除操作
                return;
            }else {
                cur = cur.next;
            }
        }
        //4.找不到
        System.out.println("没有该结点，删除失败！");
    }

    @Override
    public void removeAll(int key) {
        //删除所有key结点
        //1.检查空链表情况
        if(head == null) {
            System.out.println("链表为空，无法删除");
            return;
        }

        ListNode cur = head.next;//需要删除的结点
        ListNode prev = head;//删除结点的前一个
        //2.删除中间的结点
        while(cur != null) {
            if(cur.data != key) {
                prev = cur;//让prev走到cur的位置
                cur = cur.next;//cur往下走

            }else {
              prev.next = cur.next;
              cur = cur.next;
            }

        }
        //3.删除头结点
        if(head.data == key) {
            head = head.next;
        }

    }

    @Override
    public int size() {
        //求链表大小
        ListNode cur = head;
        int count = 0;
        while(cur!=null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        //清空链表
        head = null;
    }

    @Override
    public void show() {
        //打印
        ListNode cur = head;
        while(cur!=null) {
            System.out.print(cur.data+" ");
            cur = cur.next;
        }
    }
    public ListNode middleNode() {
        //找中间节点
        if(head == null) {
            return head;
        }

        ListNode slow = head;
        ListNode fast = head;

        while(fast!=null) {
            fast = fast.next;//先走一步
            if(fast != null) {
                fast = fast.next;
                slow = slow.next;
            }else {
                slow = slow.next;
            }
        }
        return slow;
    }
    public void  reverseList() {
        //反转链表
        if(head == null) {
            return;
        }
        ListNode cur = head.next;
        head.next = null;
        ListNode tmp = head;
        while(cur != null) {
            tmp = cur;
            cur = cur.next;
            tmp.next = head;
            head = tmp;
        }
    }
    public void partition1(int x) {
        //另一种题目
        // write code here
        if(head == null) {
            System.out.println("空链表");
            return;
        }
        System.out.println("修改前头结点为："+head.data);
        head.data = x;
        System.out.println("修改后头结点为："+x);
        ListNode cur = head.next;//遍历链表
        ListNode tmp = head;//记录新的头节点
        ListNode prev = head;
        ListNode pHead = head;
        boolean bool = true;
        while(cur != null) {
            if(cur.data < x) {
                prev.next = cur.next;
                cur.next = pHead;
                tmp = cur;
                pHead = tmp;
                cur = prev.next;
                bool = false;
                break;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(bool) {
            System.out.println("链表无需操作");
            return;
        }
        while(cur != null) {
            if(cur.data < x) {
                prev.next = cur.next;
                cur.next = head;
                tmp.next = cur;
                tmp = cur;
                cur = prev.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        head = pHead;

    }
    public void partition(int x) {
        // write code here
        if(head == null) {
            System.out.println("空链表");
            return;
        }
        if(head.next == null) {
            System.out.println("只有一个结点");
            return;
        }
        ListNode newhead1 = new ListNode(-1);//傀儡结点，放大于x的
        ListNode newhead2 = new ListNode(-1);//放小于xd的
        ListNode tmp1 = newhead1;//
        ListNode tmp2 = newhead2;
        ListNode cur = head;
        while(cur != null) {
            if(cur.data > x) {
                tmp1.next = cur;
                tmp1 = cur;
                cur = cur.next;
            }else {
                tmp2.next = cur;
                tmp2 = cur;
                cur = cur.next;
            }
        }
        tmp1.next = null;
        if(newhead2.next != null && newhead1.next != null) {
            tmp2.next = newhead1.next;
            newhead1 = null;
            head = newhead2.next;

        }else if(newhead1.next == null) {//都是小于x的
            tmp2.next = null;
            head = newhead2.next;
        }else if(newhead2.next == null) {//都是大于x的
            head = newhead1.next;
        }
    }


}
